//
//  HelpViewController.m
//  FingerUniversity
//
//  Created by vantle on 16/3/12.
//  Copyright © 2016年 vantle. All rights reserved.
//

#import "HelpViewController.h"
#import "AboutUsViewController.h"
#import "WelcomeViewController.h"
#import "AFNetworking.h"
#import "MobClick.h"
#import "SVProgressHUD.h"
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
@interface HelpViewController ()

@end

@implementation HelpViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [MobClick beginLogPageView:@"HelpViewController"];//("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"HelpViewController"];
}
- (void)viewDidLoad {
    self.viewTag=2012;
    [super viewDidLoad];
//    self.view.backgroundColor=[UIColor blackColor];
    UIImageView*bg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    bg.image=[UIImage imageNamed:@"bg"];
    [self.view addSubview:bg];
    
    UIImageView*barImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 44)];
    barImage.userInteractionEnabled=YES;
    barImage.backgroundColor=[UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1];
    [self.view addSubview:barImage];
    
    UIButton*backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame=CGRectMake(0, 0, 100, 44);
    [backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [barImage addSubview:backButton];
    
    UIImageView*backImage=[[UIImageView alloc]initWithFrame:CGRectMake(5, 12, 5, 20)];
    backImage.image=[UIImage imageNamed:@"back_arrowleft"];
    [barImage addSubview:backImage];
    
    UIImageView*logoImage=[[UIImageView alloc]initWithFrame:CGRectMake(backImage.frame.origin.x+backImage.frame.size.width+3, 6, 32, 32)];
    logoImage.layer.cornerRadius=3;
    logoImage.clipsToBounds=YES;
    logoImage.image=[UIImage imageNamed:@"app-logo-80"];
    [barImage addSubview:logoImage];
    
    UILabel*noticeLabel=[[UILabel alloc]initWithFrame:CGRectMake(logoImage.frame.origin.x+logoImage.frame.size.width+5, 10, 100, 25)];
    noticeLabel.text=@"软件设置";
    [barImage addSubview:noticeLabel];
    
    
    _tabelView=[[UITableView alloc]initWithFrame:CGRectMake(5, 64+10, ScreenWidth-10, 180)];
    _tabelView.dataSource=self;
    _tabelView.delegate=self;
    _tabelView.rowHeight=60;
    _tabelView.separatorColor=[UIColor whiteColor];
    _tabelView.backgroundColor=[UIColor clearColor];
    _tabelView.bounces=NO;
    [self.view addSubview:_tabelView];
    
    _titleArray=@[@"使用帮助",@"意见反馈",@"关于我们"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString*cellIndentifier=@"cell";
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
        UIImageView*rightArrow=[[UIImageView alloc]init];
        rightArrow.center=CGPointMake(ScreenWidth-30, cell.contentView.center.y+10);
        rightArrow.bounds=CGRectMake(0, 0, 15, 20);
        rightArrow.image=[UIImage imageNamed:@"jiantou"];
        [cell.contentView addSubview:rightArrow];
        
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.textLabel.text=_titleArray[indexPath.row];
    cell.textLabel.font=[UIFont systemFontOfSize:19];
    cell.backgroundColor=[UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:0.5];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
//        HelpUseViewController*helpUseVC=[[HelpUseViewController alloc]init];
//        [self.navigationController pushViewController:helpUseVC animated:YES];
        WelcomeViewController*welcomeVC=[[WelcomeViewController alloc]init];
        welcomeVC.viewTagQu=self.viewTag;
        [self.navigationController pushViewController:welcomeVC animated:YES];
    }
    
    else if (indexPath.row==1)
    {
        NSLog(@"意见反馈");
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSString*userId=[userDefaults objectForKey:@"userId"];
        
        self.feedbackKit = [[YWFeedbackKit alloc] initWithAppKey:@"23333568"];
        
        _feedbackKit.extInfo = @{@"loginTime":[[NSDate date] description],
                                 @"userId":userId};
#warning 自定义反馈页面配置
        _feedbackKit.customUIPlist = [NSDictionary dictionaryWithObjectsAndKeys:@"/te\'st\\Value1\"", @"testKey1", @"test<script>alert(\"error.yaochen\")</alert>Value2", @"testKey2", nil];
        
        [self _openFeedbackViewController];
    }
    else
    {
        AboutUsViewController*aboutUsVC=[[AboutUsViewController alloc]init];
        [self.navigationController pushViewController:aboutUsVC animated:YES];
    }
}
- (void)_openFeedbackViewController
{
    __weak typeof(self) weakSelf = self;
    
    [_feedbackKit makeFeedbackViewControllerWithCompletionBlock:^(YWFeedbackViewController *viewController, NSError *error) {
        if ( viewController != nil ) {
#warning 这里可以设置你需要显示的标题
            viewController.title = @"反馈界面";
            
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
            [weakSelf presentViewController:nav animated:YES completion:nil];
            
            viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:weakSelf action:@selector(actionQuitFeedback)];
            
            
            
            __weak typeof(nav) weakNav = nav;
            
            [viewController setOpenURLBlock:^(NSString *aURLString, UIViewController *aParentController) {
                UIViewController *webVC = [[UIViewController alloc] initWithNibName:nil bundle:nil];
                UIWebView *webView = [[UIWebView alloc] initWithFrame:webVC.view.bounds];
                webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
                
                [webVC.view addSubview:webView];
                [weakNav pushViewController:webVC animated:YES];
                [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:aURLString]]];
            }];
        } else {
            NSString *title = [error.userInfo objectForKey:@"msg"]?:@"接口调用失败，请保持网络通畅！";
//            [[TWMessageBarManager sharedInstance] showMessageWithTitle:title description:nil
//                                                                  type:TWMessageBarMessageTypeError];
        }
    }];
}
- (void)actionQuitFeedback
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewDidLayoutSubviews
{
    if ([_tabelView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tabelView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([_tabelView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tabelView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
